package hsp.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SmallChangeOop {
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";
    String details = "------------------零钱通明细------------------";
    double money = 0;
    double balance = 0;
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    String note;

    public void showMenu() {
        do {
            System.out.println("=============零钱通菜单==================");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消费");
            System.out.println("\t\t\t4 退出");
            System.out.println("请输入（1-4）:");
            key = scanner.next();
            //switch分支控制
            switch (key) {
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.pay();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("输入有误\n\n\n\n\n");
            }
        } while (loop);
    }

    public void detail() {
        System.out.println(details);
    }

    public void income() {
        System.out.print("收益入账金额:");
        money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("收益入账金额 需要 大于 0");
            return;//退出方法不再执行
        }
        //money校验
        balance += money;
        //拼接收益入账
        details += "\n收益入账" + "\t +" + money + "\t " + sdf.format(new Date()) + "\t" + balance;
    }

    public void pay() {
        System.out.println("请输入消费金额");
        money = scanner.nextDouble();
        if (money <= 0 || money > balance) {
            System.out.println("Cuowu");
            return;
        }
        System.out.println("消费说明:");
        note = scanner.next();
        balance -= money;
        details += "\n" + note + "\t-" + money + "\t" + sdf.format(new Date()) + "\t" + balance;
    }

    public void exit() {
        String choice = "";
        do {
            System.out.println("你确定要退出吗？y/n");
            choice = scanner.next();
        } while (!"y".equals(choice) && !"n".equals(choice));
        if ("y".equals(choice)) {
            loop = false;
        }
    }


}
